package com.mg.operating.service.impl;

import com.mg.operating.entity.ConfirmVo;
import com.mg.operating.entity.TotalVo;
import com.mg.operating.entity.VideoComments;
import com.mg.operating.entity.VideoManage;
import com.mg.operating.mapper.VideoCommentsMapper;
import com.mg.operating.mapper.VideoManageMapper;
import com.mg.operating.service.VideoManageService;
import org.springframework.stereotype.Service;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Service
public class VideoManageServiceImpl implements VideoManageService {

    //临时储存以操作用户
    private static List list = new ArrayList();

    @Resource
    VideoManageMapper videoManageMapper;

    @Resource
    VideoCommentsMapper videoCommentsMapper;

    /**
     * 用于添加点赞数，调用第一次点赞数增加，并记录点赞的视频名。
     * 第二次调用如果视频名存在则该视频以点赞过，则为取消点赞
     * @param id 要操作的视频对象id
     * @return 操作成功返回1，失败返回0
     */
    public int like(int id) {
        VideoManage videoManage = videoManageMapper.getId(id);
        int i = 0;
        if (!list.contains(videoManage.getName())) {
            list.add(videoManage.getName());
            videoManage.setLikes(videoManage.getLikes() + 1);
            i = videoManageMapper.upData(videoManage);
        }else {
            list.remove(videoManage.getName());
            videoManage.setLikes(videoManage.getLikes() - 1);
            i = videoManageMapper.upData(videoManage);
        }
        return i;
    }

    /**
     * 视频转发数，调用转发数并成功则增加1
     * 如果id为传入的id,confirm为0,则返回转发路径
     * 如果id为传入的id,confirm为1,表示确认转发,则跳转并返回address="1"
     * @param id 要操作的视频对象id
     * @param confirm 判断是否确认转发值1为确认
     * @return 操作成功返回1，失败返回0
     */
    public ConfirmVo forWardPlus(int id, int confirm) {
        ConfirmVo confirmVo = new ConfirmVo();
        if (confirm==1){
            new ModelAndView("redirect:https://qzone.qq.com/");
            VideoManage videoManage = videoManageMapper.getId(id);
            videoManage.setForward(videoManage.getForward() + 1);
            int i = videoManageMapper.upData(videoManage);
            confirmVo.setId(id);
            confirmVo.setAddress(i+"");
            return confirmVo;
        }
        confirmVo.setId(id);
        confirmVo.setAddress("https://qzone.qq.com/");
        return confirmVo;
    }

    /**
     * 通过id获取视频信息与其相关的评论信息，通过两个单表查询合并,并重新统计评论数
     * getId: 查询数据
     * count: 统计数据
     * getUid: 获取评论数据
     * @param id 操作对象id
     * @return 返回查询信息
     */
    public VideoManage getVideo(int id){
        VideoManage video = videoManageMapper.getId(id);
        video.setCom_number(videoCommentsMapper.count(id));
        video.setListComments(videoCommentsMapper.getUid(id));
        return video;
    }

    /**
     * 删除视频及其相关信息
     * deleteById: 通过id删除主信息
     * delByUid: 通过uid删除所有附属信息(主信息的id为副信息的uid)
     * @param id
     * @return
     */
    public int delete(int id){
        int i = videoManageMapper.delId(id);
        int i1 = videoCommentsMapper.delByUid(id);
        if (i1!=0&&i==1){
            return 1;
        }
        return 0;
    }

    /**
     * 统计视频总数，点赞总数，评论总数，转发总数
     * @return
     */
    public TotalVo count(){
        TotalVo totalVo = new TotalVo();
        totalVo.setCountId(videoManageMapper.countId());
        totalVo.setCountLike(videoManageMapper.countlike());
        totalVo.setCountComment(videoManageMapper.countcomment());
        totalVo.setCountForward(videoManageMapper.countforward());
        return totalVo;
    }

}
